package com.yyt.module_main.act;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yyt.libsrc.bean.EarningsHistoryBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.LocalHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityIncomListBinding;
import com.yyt.module_main.databinding.ItemIncomeListBinding;
import com.yyt.module_main.vm.IncomeLsitVM;

import top.lixb.libcommon.base.ItemHolder;
import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.base.SimpleBindingAdapter;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_income_list)
public class IncomListActivity extends RefreshableActivity<ActivityIncomListBinding> {

    private IncomeLsitVM mVm;
    private View mConditionView;
    private SimpleBindingAdapter<EarningsHistoryBean.DataBean.ListBean, ItemIncomeListBinding> mAdapter;

    @Override
    protected void init() {
        mVm = new IncomeLsitVM();
        mRefreshContentBinding.setVm(mVm);
        setTitle("查看详情");

//        initConditionChooseView();
//        mTvRight.setText("筛选");
//        mTvRight.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // TODO: 2018/8/3 弹出筛选框
//                PopupWindow popupWindow = new PopupWindow();
//                popupWindow.setContentView(mConditionView);
//                popupWindow.showAsDropDown(mToolbar);
//            }
//        });

        if (LocalHelper.getMyRole() == 3) {
            mRefreshContentBinding.rb3.setVisibility(View.VISIBLE);
            mRefreshContentBinding.rb0.setVisibility(View.VISIBLE);
            mRefreshContentBinding.rb0.setChecked(true);
            mRefreshContentBinding.rb1.setChecked(false);
        } else {
            mRefreshContentBinding.rb0.setVisibility(View.GONE);
            mRefreshContentBinding.rb0.setChecked(false);
            mRefreshContentBinding.rb1.setChecked(true);
            mRefreshContentBinding.rb3.setVisibility(View.INVISIBLE);
        }

        mRefreshContentBinding.rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                getData(true);
            }
        });

        mRefreshContentBinding.rv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SimpleBindingAdapter<EarningsHistoryBean.DataBean.ListBean, ItemIncomeListBinding>(R.layout.item_income_list) {
            @Override
            public void bind(@NonNull ItemHolder<ItemIncomeListBinding> holder, EarningsHistoryBean.DataBean.ListBean bean) {
                if (bean.type == 1) {
                    holder.getBinding().tvTitle.setText("代理商费用");
                } else if (bean.type == 2) {
                    holder.getBinding().tvTitle.setText("收益");
                }
                holder.getBinding().tvTime.setText(bean.createTime);
                holder.getBinding().tvAmount.setText("+" + StringUtils.doubleFormat(String.valueOf(bean.amount)));
            }

        };
        mRefreshContentBinding.rv.setAdapter(mAdapter);
    }

    private void initConditionChooseView() {
        mConditionView = View.inflate(this, R.layout.layout_condition_choose, null);
    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_incom_list;
    }

    @Override
    protected void getData(boolean refresh) {
        int checkedRadioButtonId = mRefreshContentBinding.rgroup.getCheckedRadioButtonId();
        if (checkedRadioButtonId == R.id.rb0) { //全部
            mVm.type.set(3);
        } else if (checkedRadioButtonId == R.id.rb1) {//收益
            mVm.type.set(2);
        } else if (checkedRadioButtonId == R.id.rb3) {//代理商费用
            mVm.type.set(1);
        } else {
            mVm.type.set(3);
        }
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.earningsHistory)
                .addP("type", String.valueOf(mVm.type.get()))
//                .addP("startTime", String.valueOf(mVm.startTime.get()))
//                .addP("endTime", String.valueOf(mVm.endTime.get()))
                .build(), new SuccessCallback<EarningsHistoryBean>() {
            @Override
            public void onSuccess(EarningsHistoryBean earningsHistoryBean) {
                mAdapter.setNewData(earningsHistoryBean.data.list);
            }

            @Override
            public void onComplete() {
                super.onComplete();
                finishGetData();
            }
        });
    }
}
